package cc.abanlee.framework.security.core.handler;

import cc.abanlee.framework.common.exception.enums.GlobalErrorCodeConstants;
import cc.abanlee.framework.common.pojo.CommonResult;
import cc.abanlee.framework.common.util.servlet.ServletUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author: create by
 * @version: v1.0
 * @description:
 * @date:2022/8/31
 */
@Slf4j
public class CustomAccessDeniedHandler implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        log.debug("[commence][访问 URL({}) 时，没有权限]", request.getRequestURI());

        ServletUtils.writeJSON(response, CommonResult.error(GlobalErrorCodeConstants.FORBIDDEN));
    }
}
